package com.intellagric.service.traceability.Impl.trace_field;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.TraceFieldTypeMapper;
import com.intellagric.pojo.TraceField;
import com.intellagric.pojo.TraceFieldType;
import com.intellagric.pojo.TraceFieldTypeExample;
import com.intellagric.pojo.TraceProduceCategory;
import com.intellagric.service.traceability.trace_field.FieldTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/11/6
 * @DESC： 场地类型信息服务
 */
@Service
public class FieldTypeServiceImpl implements FieldTypeService {

    @Autowired
    private TraceFieldTypeMapper fieldTypeMapper;

    /**
     * 添加场地类型信息
     * @param fieldType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addFieldType(TraceFieldType fieldType) {
        if (fieldTypeMapper.insertSelective(fieldType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除场地类型信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteFieldType(String id) {
        if (fieldTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改场地类型信息
     * @param fieldType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editFieldType(TraceFieldType fieldType) {
        if (fieldTypeMapper.updateByPrimaryKeySelective(fieldType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询场地类型信息
     * @param id
     * @return TraceFieldType
     */
    @Override
    public TraceFieldType getFieldTypeById(String id) {
        return fieldTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据parentId查询场地类型信息
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getFieldTypeByParentId(String parentId) {
        TraceFieldTypeExample fieldTypeExample = new TraceFieldTypeExample();
        fieldTypeExample.setOrderByClause("sort");
        fieldTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<TraceFieldType> fieldTypeList = fieldTypeMapper.selectByExample(fieldTypeExample);
        return LayuiDataGridResult.success().add(fieldTypeList,fieldTypeList.size());
    }

    /**
     * 查询场地类型信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getFieldTypeList() {
        TraceFieldTypeExample fieldTypeExample = new TraceFieldTypeExample();
        fieldTypeExample.setOrderByClause("sort");
        fieldTypeExample.createCriteria().andIdIsNotNull();
        List<TraceFieldType> fieldTypeList = fieldTypeMapper.selectByExample(fieldTypeExample);
        return LayuiDataGridResult.success().add(fieldTypeList,fieldTypeList.size());
    }

    /**
     * 查询场地类型信息列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getFieldTypeTree() {
        List<TraceFieldType> fieldTypeList = this.getFieldTypeList().getData();
        List<TraceFieldType> rootFieldTypeList = this.getFieldTypeByParentId("0").getData();
        for (TraceFieldType fieldType : rootFieldTypeList) {
            fieldType.setChildren(getChild(fieldType.getId(), fieldTypeList));
        }
        return LayuiDataGridResult.success().add(rootFieldTypeList,rootFieldTypeList.size());
    }
    /**
     * 查询场地类型信息列表，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getFieldTypeTree(String createBy) {
        TraceFieldTypeExample example=new TraceFieldTypeExample();
        example.createCriteria().andCreateByEqualTo(createBy);
        List<TraceFieldType> fieldTypeList=fieldTypeMapper.selectByExample(example);
        example.createCriteria().andCreateByEqualTo(createBy).andParentIdEqualTo("0");
        List<TraceFieldType> rootFieldTypeList = fieldTypeMapper.selectByExample(example);
        for (TraceFieldType fieldType : rootFieldTypeList) {
            fieldType.setChildren(getChild(fieldType.getId(), fieldTypeList));
        }
        return LayuiDataGridResult.success().add(rootFieldTypeList,rootFieldTypeList.size());
    }

    private List<TraceFieldType> getChild(String id, List<TraceFieldType> fieldTypeList) {
        // 子菜单
        List<TraceFieldType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (TraceFieldType fieldType : fieldTypeList) {
            if (fieldType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (fieldType.getParentId().equals(id)) {
                    childList.add(fieldType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (TraceFieldType fieldType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            fieldType.setChildren(getChild(fieldType.getId(), fieldTypeList));
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
